package com.znyj.cloud.device.executor;

import com.znyj.cloud.device.async.ReportAsync;
import com.znyj.cloud.device.constant.SysConstant;
import com.znyj.cloud.device.entity.ReportEntity;
import com.znyj.cloud.device.pack.ReportReceivePackage;
import org.apache.log4j.Logger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Timestamp;

/**
 * Created by gaoliwei02 on 17/5/29.
 */
@Component
public class ReportExecutorService {
    private static final Logger logger = Logger.getLogger(ReportExecutorService.class);

    @Resource
    private ThreadPoolTaskExecutor taskExecutor;

    public void executeUpdateReport(ReportReceivePackage reportReceivePackage, String clientIp) {
        ReportEntity reportEntity = new ReportEntity();
        reportReceivePackage.updateReportEntity(reportEntity, clientIp);
        taskExecutor.execute(new ReportAsync(reportEntity, SysConstant.CDB_OP.UPDATE_REPORT));
    }

    public void executeFirstInsert(ReportReceivePackage reportReceivePackage) {
        ReportEntity reportEntity = new ReportEntity();
        reportReceivePackage.insertFirstEntity(reportEntity);
        taskExecutor.execute(new ReportAsync(reportEntity, SysConstant.CDB_OP.INSERT_FIRST));
    }

    public void executeOffline(String deviceId) {
        ReportEntity reportEntity = new ReportEntity();
        reportEntity.setDeviceId(deviceId);
        reportEntity.setOnlineStatus("offline");
        taskExecutor.execute(new ReportAsync(reportEntity, SysConstant.CDB_OP.UPDATE_OFFLINE));
    }

    public void executeUpdateOtaTime(String deviceId) {
        ReportEntity reportEntity = new ReportEntity();
        reportEntity.setDeviceId(deviceId);
        reportEntity.setOtaTime(new Timestamp(System.currentTimeMillis()));
        taskExecutor.execute(new ReportAsync(reportEntity, SysConstant.CDB_OP.UPDATE_OTA_TIME));
    }

    public void executeUpdatePoiid(String deviceId) {
        ReportEntity reportEntity = new ReportEntity();
        reportEntity.setDeviceId(deviceId);
        taskExecutor.execute(new ReportAsync(reportEntity, SysConstant.CDB_OP.UPDATE_POIID));
    }
}
